import ACCESS_ENUM from '@/router/AccessEnum.ts'

/**
 * 检查当前登录用户的角色是否满足需求。
 * @param loginUser
 * @param needRole
 */
const checkRole = (loginUser, needRole, path = '') => {
  const roleHierarchy = [
    ACCESS_ENUM.GUEST, // 最低权限：访客
    ACCESS_ENUM.USER, // 普通用户
    ACCESS_ENUM.ADMIN, // 管理员
  ]

  const loginUserRole = loginUser?.userRole ?? ACCESS_ENUM.GUEST

  const hasEnoughRole = roleHierarchy.indexOf(loginUserRole) >= roleHierarchy.indexOf(needRole)
  console.log(
    'loginUserRole: ',
    loginUserRole,
    'needRole: ',
    needRole,
    'hasEnoughRole: ',
    hasEnoughRole,
    'path:',
    path,
  )
  return hasEnoughRole
}

export default checkRole
